/****************************************************************************/
/***                                                                      ***/
/***   (C) 2012-2014 Dierk Ohlerich et al., all rights reserved.          ***/
/***                                                                      ***/
/***   Released under BSD 2 clause license, see LICENSE.TXT               ***/
/***                                                                      ***/
/****************************************************************************/

mtrl ComputeShader 
{
    cbuffer csb0 : cs(0) : register(0)
    {
        row_major float3x4 Matrix;
        float3 PosMul;
        float3 PosAdd;
        float3 tx;
        float3 ty;
        float Size;
    };

    cs hlsl5
    {
        float map(float3 p)
        {
            return length(float2(length(p.xz)-1.5,p.y))-0.5;
//            return length(p)-1.5;
        }

        RWByteAddressBuffer vb : register(u0);
        RWByteAddressBuffer ib : register(u1);
        RWStructuredBuffer<uint> vc : register(u2);
        RWStructuredBuffer<uint> ic : register(u3);

        [numthreads(4,4,4)]
        void main(uint3 tid:SV_DispatchThreadID)
        {
            float3 pos = ((float3)(tid)+PosAdd)*PosMul;
            float3 posm = mul(Matrix,float4(pos,1));
            float f = map(posm);
            if(f<0)
            {
                uint vo = vc.IncrementCounter()*4;
                uint vp = vo*12;
                vb.Store3(vp,asuint((-tx-ty)*Size+pos)); vp+=12;
                vb.Store3(vp,asuint(( tx-ty)*Size+pos)); vp+=12;
                vb.Store3(vp,asuint(( tx+ty)*Size+pos)); vp+=12;
                vb.Store3(vp,asuint((-tx+ty)*Size+pos)); vp+=12;

                uint ip = ic.IncrementCounter()*2*12;
                ib.Store3(ip,uint3(vo+0,vo+1,vo+2)); ip += 12;
                ib.Store3(ip,uint3(vo+0,vo+2,vo+3)); ip += 12;
            }
        }
    };
} 

mtrl WriteCount
{
    cs hlsl5
    {
        RWStructuredBuffer<uint> cnt : register(u0);
        RWBuffer<uint> ind : register(u1);

        [numthreads(1,1,1)]
        void main(uint3 tid:SV_DispatchThreadID)
        {
            ind[0] = cnt.IncrementCounter()*6;
            ind[1] = 1;
            ind[2] = 0;
            ind[3] = 0;
            ind[4] = 0;
        }
    };
}

/****************************************************************************/
